/*
 * Copyright (c) 2011, Michael van der Westhuizen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    - Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    - Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef PYXMLSEC1_XMLDSIG_C_MCVDW_20110216
#define PYXMLSEC1_XMLDSIG_C_MCVDW_20110216

#include "pyxmlsec.h"

#include <xmlsec/xmldsig.h>



PyObject * xmlsec1_xmlSecDSigCtxCreate(PyObject * self, PyObject * arg)
{
    xmlSecKeysMngrPtr keysMngr;
    xmlSecDSigCtxPtr ptr;
    PyObject * ret;

    if (NULL == (keysMngr = xmlsec1mod_unwrap_xmlSecKeysMngrPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecKeysMngrPtr);
        return NULL;
    }

    if (NULL == (ptr = xmlSecDSigCtxCreate(keysMngr)))
    {
        ENSURE_EXCEPTION_SET(xmlSecDSigCtxCreate);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecDSigCtxPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecDSigCtxPtr);
        xmlSecDSigCtxDestroy(ptr);
        return NULL;
    }

    return ret;
}



PyObject * xmlsec1_xmlSecDSigCtxDestroy(PyObject * self, PyObject * arg)
{
    xmlSecDSigCtxPtr dsigCtx;

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    xmlSecDSigCtxDestroy(dsigCtx);
    if (PyErr_Occurred())
    {
        ENSURE_EXCEPTION_SET(xmlSecDSigCtxDestroy);
        return NULL;
    }

    Py_RETURN_NONE;
}


PyObject * xmlsec1_xmlSecDSigCtxVerify(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecDSigCtxPtr dsigCtx;
    xmlNodePtr node;

    static char * kwlist[] = {"dsigCtx", "node", NULL};

    arg1 = arg2 = NULL;
    dsigCtx = NULL;
    node = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    if (NULL == (node = xmlsec1mod_unwrap_xmlNodePtr(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlNodePtr);
        return NULL;
    }

    if (0 != xmlSecDSigCtxVerify(dsigCtx, node))
    {
        ENSURE_EXCEPTION_SET(xmlSecDSigCtxVerify);
        return NULL;
    }

    Py_RETURN_NONE;
}



PyObject * xmlsec1_xmlSecDSigCtxGetStatus(PyObject * self, PyObject * arg)
{
    xmlSecDSigCtxPtr dsigCtx;

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    return Py_BuildValue("i", dsigCtx->status);
}



PyObject * xmlsec1_xmlSecDSigCtxSetEnabledReferenceUris(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    unsigned int enabledReferenceUris;
    xmlSecDSigCtxPtr dsigCtx;

    static char * kwlist[] = {"dsigCtx", "enabledReferenceUris", NULL};

    arg1 = NULL;
    dsigCtx = NULL;
    enabledReferenceUris = xmlSecTransformUriTypeNone;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI", kwlist, &arg1, &enabledReferenceUris))
    {
        return NULL;
    }

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    dsigCtx->enabledReferenceUris = enabledReferenceUris;
    Py_RETURN_NONE;
}



PyObject * xmlsec1_xmlSecDSigCtxGetEnabledReferenceUris(PyObject * self, PyObject * arg)
{
    xmlSecDSigCtxPtr dsigCtx;

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    return Py_BuildValue("I", dsigCtx->enabledReferenceUris);
}


PyObject * xmlsec1_xmlSecDSigCtxEnableSignatureTransform(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecDSigCtxPtr dsigCtx;
    xmlSecTransformId transformId;

    static char * kwlist[] = {"dsigCtx", "transformId", NULL};

    arg1 = arg2 = NULL;
    dsigCtx = NULL;
    transformId = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    if (NULL == (transformId = xmlsec1mod_unwrap_xmlSecTransformId(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecTransformId);
        return NULL;
    }

    if (0 != xmlSecDSigCtxEnableSignatureTransform(dsigCtx, transformId))
    {
        ENSURE_EXCEPTION_SET(xmlSecDSigCtxEnableSignatureTransform);
        return NULL;
    }

    Py_RETURN_NONE;
}



PyObject * xmlsec1_xmlSecDSigCtxEnableReferenceTransform(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    xmlSecDSigCtxPtr dsigCtx;
    xmlSecTransformId transformId;

    static char * kwlist[] = {"dsigCtx", "transformId", NULL};

    arg1 = arg2 = NULL;
    dsigCtx = NULL;
    transformId = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &arg1, &arg2))
    {
        return NULL;
    }

    if (NULL == (dsigCtx = xmlsec1mod_unwrap_xmlSecDSigCtxPtr(arg1)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecDSigCtxPtr);
        return NULL;
    }

    if (NULL == (transformId = xmlsec1mod_unwrap_xmlSecTransformId(arg2)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_unwrap_xmlSecTransformId);
        return NULL;
    }

    if (0 != xmlSecDSigCtxEnableReferenceTransform(dsigCtx, transformId))
    {
        ENSURE_EXCEPTION_SET(xmlSecDSigCtxEnableReferenceTransform);
        return NULL;
    }

    Py_RETURN_NONE;
}

#if 0
#endif

#endif
